package com.zn.web.controller;

//import com.zn.entity.Order;
import com.csvreader.CsvWriter;
import com.zn.feignClient.UserServerClient;
import com.zn.socket.SocketHandler;
import com.zn.util.DateUtils;
import com.zn.util.UUIDUtils;
import com.zn.vo.AccountInfo;
import com.zn.vo.Message;
import com.zn.vo.ReportResponse;
import com.zn.web.dto.ReportDto;
import com.zn.web.rpc.TradeRemoteCall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.*;


@RestController
@RequestMapping("/orderCont")
public class OrderController {
    @Autowired
    private TradeRemoteCall tradeRemoteCall;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public void login(){
//        AccountInfo accountInfo= tradeRemoteCall.login(133562369L, "123456");
//        System.err.println("toString........."+accountInfo.toString());
    }
    @RequestMapping(value = "/weituo",method = RequestMethod.POST)
    public void weituo(){
        Map<String,Object> parmMap=new HashMap<String,Object>();
        parmMap.put("id",1);
        parmMap.put("inst","10001328");
        parmMap.put("dir",0);
        parmMap.put("of",0);
        parmMap.put("vol",1);
        parmMap.put("price",0.3649);
        parmMap.put("cookie","10442890163403493935");
//        ReportResponse reportResponse= tradeRemoteCall.reportOrder(parmMap);
//        System.err.println("toString........."+reportResponse.toString());
    }

    @RequestMapping(value = "/report",method = RequestMethod.POST)
    public void report(@RequestBody ReportDto reportDto){
        System.err.println("report....start...............");
        String nowDate= DateUtils.convertDateToString(new Date());
        String filePath = "D:\\orders\\";
        String fileName="order_"+DateUtils.convertDateToString(new Date())+"."+DateUtils.getDateTime("yyyyMMddHHmmssSSS",new Date())+".csv";
        // 创建CSV写对象
        try {
            CsvWriter csvWriter = new CsvWriter(filePath+fileName,',', Charset.forName("GBK"));
            //写头列
            String[] headers = {"local_entrust_no","fund_account","exchange_type","stock_code","entrust_bs","entrust_prop","entrust_price","entrust_amount","batch_no","asset_prop","entrust_type","cashgroup_prop","compact_id","client_filed1","clientfield2"};
            String[] content={reportDto.getLocalEntrustNo(),reportDto.getFundAccount(),reportDto.getExchangeType(),reportDto.getStockCode(),reportDto.getEntrustBs(),reportDto.getEntrustProp(),reportDto.getEntrustPrice()
                            ,reportDto.getEntrustAmount(),reportDto.getBatchNo(),reportDto.getAssetProp(),reportDto.getEntrustType(),reportDto.getCashgroupProp(),reportDto.getCompactId(),
                            reportDto.getClientFiled1(),reportDto.getClientField2()};
            csvWriter.writeRecord(headers);
            csvWriter.writeRecord(content);
            csvWriter.close();

            /*// 写表头
            File file = new File(filePath);
            if(!file.exists()){

            }else{
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath,true),"GBK"),1024);
                CsvWriter csvWriter =  new CsvWriter(out,',');
                String[] content = {String.valueOf(UUIDUtils.getMathRandom()),productName(), String.valueOf((Math.random()*90+10))};
                csvWriter.writeRecord(content);
                csvWriter.close();
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}